package com.example.loginproject.tool

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.view.View.MeasureSpec
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.example.loginproject.R
import kotlin.math.PI

fun Fragment.navigateTo(
    target:Fragment,
    enterAnim:Int = R.anim.enter_from_right,
    exitAnim:Int = R.anim.exit_to_left,
    popEnter:Int = R.anim.pop_enter_from_left,
    popExit:Int = R.anim.pop_exit_to_right,
    addToStack:Boolean=true){
    parentFragmentManager.commit {
        setCustomAnimations(enterAnim,exitAnim,popEnter,popExit)
        replace(R.id.fragmentContainerView,target)
        setReorderingAllowed(true)
        if (addToStack){
            addToBackStack(null)
        }
    }
}
fun View.getMode(measureSpec:Int)=MeasureSpec.getMode(measureSpec)
fun View.getSize(measureSpec:Int)=MeasureSpec.getMode(measureSpec)

//px转化为dp
fun Context.px2dp(px:Int)= (px /resources.displayMetrics.density).toInt()
//dp转化成px
fun Context.dp2px(dp:Int) = (resources.displayMetrics.density*dp).toInt()
//将sp转化成px
fun Context.sp2px(sp:Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,resources.displayMetrics)

//将一个颜色添加一个透明度
@RequiresApi(Build.VERSION_CODES.O)
fun colorToAlphaColor(alpha:Int,color:Int):Int{
    //将int类型颜色转化为color
   return  if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
          Color.argb(alpha,Color.red(color),Color.green(color),Color.blue(color))
    }else{
        color
   }
}
fun degreeToRadian(degree:Float):Double{
    return degree*Math.PI/180
}